package com.amumu.drama.common.modules.invitation.service.impl;

import com.amumu.drama.common.base.api.CommonResult;
import com.amumu.drama.common.modules.invitation.dto.NewMemberDto;
import com.amumu.drama.common.modules.invitation.service.InvitationNewMemberService;
import com.amumu.drama.common.utils.CodeGenUtils;
import com.amumu.drama.mapper.InvitationNewMemberMapper;
import com.amumu.drama.model.InvitationNewMember;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Auther: DM
 * @Date: 2024/6/11 10:40
 * @Description:
 */
@Service
@Log4j2
public class InvitationNewMemberServiceImpl extends ServiceImpl<InvitationNewMemberMapper, InvitationNewMember> implements InvitationNewMemberService {

    @Autowired
    private InvitationNewMemberMapper invitationNewMemberMapper;

    @Override
    public CommonResult<String> generateInvitationNewMemberCode(NewMemberDto newMemberDto,String userId) {
        try {
            QueryWrapper<InvitationNewMember> invitationNewMemberQueryWrapper = new QueryWrapper<>();
            invitationNewMemberQueryWrapper.eq("new_member_id", newMemberDto.getNewMemberId());
            invitationNewMemberQueryWrapper.eq("old_member_id", newMemberDto.getOldMemberId());
            InvitationNewMember existInvitation = invitationNewMemberMapper.selectOne(invitationNewMemberQueryWrapper);
            if (existInvitation != null && existInvitation.getStatus() == 0) {
                return CommonResult.success(existInvitation.getCode());
            }
            InvitationNewMember invitation = new InvitationNewMember();
            invitation.setCreateTime(LocalDateTime.now());
            invitation.setStatus(0);
            invitation.setCreator(userId);
            invitation.setCode(CodeGenUtils.generateInvitationCode(7));
            invitation.setNewMemberId(newMemberDto.getNewMemberId());
            invitation.setOldMemberId(newMemberDto.getOldMemberId());
            invitationNewMemberMapper.insert(invitation);
            return CommonResult.success(invitation.getCode());
        } catch (Exception e) {
            log.error("创建邀请码失败", e);
            return CommonResult.failed("创建邀请码失败 " + e.getMessage());
        }
    }

    @Override
    public CommonResult<String> validateCode(String invitationCode) {
        if (StringUtils.isBlank(invitationCode)) {
            return CommonResult.failed("validateCode:{} 参数不存在");
        }
        try {
            QueryWrapper<InvitationNewMember> invitationAnchorQueryWrapper = new QueryWrapper<>();
            invitationAnchorQueryWrapper.eq("code", invitationCode);
            InvitationNewMember invitation = invitationNewMemberMapper.selectOne(invitationAnchorQueryWrapper);
            if (invitation == null) {
                return CommonResult.success("邀请码不存在");
            }
            // 检查是否启用
            if (invitation.getStatus() == 1) {
                return CommonResult.success("邀请码已使用");
            }
            return CommonResult.success();
        } catch (Exception e) {
            return CommonResult.failed("请稍后重试");
        }
    }
}
